/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.cuahangDAO;
import dao.khuvucDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import pojo.Cuahang;
import pojo.Hanghoa;
import pojo.Khuvuc;
import pojo.Loaihanghoa;

/**
 *
 * @author HoaDinhGiang
 */
public class QuanLyCuaHangInternalFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form QuanLyCuaHangInternalFrame
     */
    private DefaultComboBoxModel comboBoxModelKhuVuc;
    private DefaultTableModel dtm;
    private static Cuahang _teampObjectCuaHang;
    private List<Cuahang> dsCuaHang;
    private int _maCuaHang;
    public QuanLyCuaHangInternalFrame() {
        initComponents();
        this.dtm = new DefaultTableModel();
        this.dtm.addColumn("Mã");
        this.dtm.addColumn("Tên Cửa Hàng");
        this.dtm.addColumn("Khu Vực");
        this.dtm.addColumn("Địa Chỉ");
        this.dtm.addColumn("Điện Thoại");
        this.dtm.addColumn("Fax");
        this.dtm.addColumn("Email");
        this.dtm.addColumn("Website");
        this.dtm.addColumn("Mã Số Thuế");
        this.dtm.addColumn("Số Tài Khoản");
        this.dtm.addColumn("Ngân Hàng");
    }

    public int indexCbKhuVucTuTable(Cuahang ch) {
        int ix = 0;
        for (int i = 0; i < this.jcbKhuVuc.getItemCount(); i++) {
            Khuvuc kv = (Khuvuc) jcbKhuVuc.getItemAt(i);
            if (kv.getMakhuvuc() == ch.getKhuvuc().getMakhuvuc()) {
                return i;
            }
        }
        return ix;
    }
    public boolean KiemTraThongTinNhap() {
        boolean flag = true;
        if (jtfTenCuaHang.getText().length() == 0 || jtfDiaChi.getText().length() == 0
                || jtfMaSoThue.getText().length() == 0 || jtfFax.getText().length() == 0
                || jtfNganHang.getText().length() == 0 || jtfSoTaiKhoan.getText().length() == 0
                || jtfEmail.getText().length() == 0 || jtfDienThoai.getText().length() == 0) {
            return false;
        }
        if ("".equals(jtfTenCuaHang.getText()) || "".equals(jtfDiaChi.getText())
                || "".equals(jtfMaSoThue.getText()) || "".equals(jtfFax.getText())
                || "".equals(jtfNganHang.getText()) || "".equals(jtfSoTaiKhoan.getText())
                || "".equals(jtfEmail.getText()) || "".equals(jtfDienThoai.getText())) {
            return false;
        }
        return flag;
    }

    public void loadcomboxKhuVuc() {
        try {
            this.comboBoxModelKhuVuc = new DefaultComboBoxModel();
            this.jcbKhuVuc.setModel(comboBoxModelKhuVuc);
            List<Khuvuc> ds = khuvucDAO.layDanhSachKhuVuc();
            for (int i = 0; i < ds.size(); i++) {
                Khuvuc dm = ds.get(i);
                this.comboBoxModelKhuVuc.addElement(dm);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void hienThi() {
        jtbDanhSachCuaHang.removeAll();
        dsCuaHang = cuahangDAO.layDanhSachCuaHangTonTai();
        dtm.setNumRows(dsCuaHang.size());
        for (int i = 0; i < dsCuaHang.size(); i++) {
            Cuahang ch = dsCuaHang.get(i);
            dtm.setValueAt(ch.getMacuahang(), i, 0);
            dtm.setValueAt(ch.getTencuahang(), i, 1);
            dtm.setValueAt(ch.getKhuvuc().getTenkhuvuc(), i, 2);
            dtm.setValueAt(ch.getDiachi(), i, 3);
            dtm.setValueAt(ch.getDienthoai(), i, 4);
            dtm.setValueAt(ch.getFax(), i, 5);
            dtm.setValueAt(ch.getEmail(), i, 6);
            dtm.setValueAt(ch.getWebsite(), i, 7);
            dtm.setValueAt(ch.getMasothue(), i, 8);
            dtm.setValueAt(ch.getSotaikhoan(), i, 9);
            dtm.setValueAt(ch.getNganhang(), i, 10);
        }
        this.jtbDanhSachCuaHang.setModel(dtm);
        this.jtbDanhSachCuaHang.changeSelection(0, 0, false, false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfTenCuaHang = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtfEmail = new javax.swing.JTextField();
        jcbKhuVuc = new javax.swing.JComboBox();
        jtfDiaChi = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSachCuaHang = new javax.swing.JTable();
        jbtThoat = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jtfFax = new javax.swing.JTextField();
        jtfWebsite = new javax.swing.JTextField();
        jtfDienThoai = new javax.swing.JTextField();
        jtfSoTaiKhoan = new javax.swing.JTextField();
        jtfNganHang = new javax.swing.JTextField();
        jtfMaSoThue = new javax.swing.JTextField();
        jbtSua = new javax.swing.JButton();
        jbtXoa = new javax.swing.JButton();
        jbtThem = new javax.swing.JButton();
        jbtLoad = new javax.swing.JButton();
        jbtTimKiem = new javax.swing.JButton();
        jbtClear = new javax.swing.JButton();
        jtfMaCuaHang = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Quản Lý Cửa Hàng");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setText("Tên Cửa Hàng:");

        jLabel2.setText("Mã Cửa Hàng:");

        jLabel3.setText("Khu Vực:");

        jcbKhuVuc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbKhuVucActionPerformed(evt);
            }
        });

        jLabel4.setText("Địa Chỉ:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cửa Hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));

        jtbDanhSachCuaHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbDanhSachCuaHang.setCellSelectionEnabled(true);
        jtbDanhSachCuaHang.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jtbDanhSachCuaHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbDanhSachCuaHangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbDanhSachCuaHang);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        jLabel5.setText("Fax:");

        jLabel6.setText("Email:");

        jLabel7.setText("Website:");

        jLabel8.setText("Mã Số Thuế:");

        jLabel9.setText("Ngân Hàng:");

        jLabel10.setText("Số Tài Khoản:");

        jLabel11.setText("Điện Thoại:");

        jbtSua.setText("Cập Nhật");
        jbtSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSuaActionPerformed(evt);
            }
        });

        jbtXoa.setText("Xóa");
        jbtXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaActionPerformed(evt);
            }
        });

        jbtThem.setText("Thêm");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jbtLoad.setText("Tải Lại");
        jbtLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLoadActionPerformed(evt);
            }
        });

        jbtTimKiem.setText("Tìm Kiếm");
        jbtTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtTimKiemActionPerformed(evt);
            }
        });

        jbtClear.setText("Clear All");
        jbtClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtThem, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jbtXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jbtSua, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(151, 151, 151)
                .addComponent(jbtThoat)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(39, 39, 39)
                                .addComponent(jtfDiaChi))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfTenCuaHang))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtfMaCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(122, 122, 122)
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jcbKhuVuc, 0, 172, Short.MAX_VALUE)
                                .addGap(62, 62, 62)
                                .addComponent(jLabel8)
                                .addGap(31, 31, 31)
                                .addComponent(jtfMaSoThue, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel11))
                                        .addGap(22, 22, 22)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jtfDienThoai)
                                            .addComponent(jtfFax))
                                        .addGap(64, 64, 64)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel6)
                                                .addGap(32, 32, 32)
                                                .addComponent(jtfEmail))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel9)
                                                .addGap(4, 4, 4)
                                                .addComponent(jtfNganHang)))
                                        .addGap(62, 62, 62))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jbtClear)
                                        .addGap(531, 531, 531)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jbtTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jbtLoad))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addGap(50, 50, 50)
                                        .addComponent(jtfWebsite, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addGap(27, 27, 27)
                                        .addComponent(jtfSoTaiKhoan, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)))))
                        .addGap(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jtfMaCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(jcbKhuVuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel8)
                                .addComponent(jtfMaSoThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1))
                    .addComponent(jtfTenCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel4))
                    .addComponent(jtfDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(jtfDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfWebsite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7))))
                        .addGap(3, 3, 3)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfSoTaiKhoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtfNganHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(3, 3, 3)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel5)
                                                .addGap(3, 3, 3))
                                            .addComponent(jLabel9)))
                                    .addComponent(jLabel10)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jtfFax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbtLoad, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtTimKiem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtThem, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtSua, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void jbtSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSuaActionPerformed
        // TODO add your handling code here:
        if (KiemTraThongTinNhap() == false) {
            JOptionPane.showMessageDialog(this, "Chưa Nhập Đủ Thông Tin\nChỉ duy nhất mục Website có thể bỏ trống.", "Thông Báo", JOptionPane.WARNING_MESSAGE);
            return;
        }
        int maCuaHang = Integer.parseInt(jtfMaCuaHang.getText());
        Cuahang ch = cuahangDAO.layThongTinCuaHang(maCuaHang);

        if (ch != null) {
            ch.setKhuvuc(khuvucDAO.layKhuVucTheoTen(this.jcbKhuVuc.getSelectedItem().toString()));
            ch.setTencuahang(jtfTenCuaHang.getText());
            ch.setDiachi(jtfDiaChi.getText());
            ch.setEmail(jtfEmail.getText());
            ch.setDienthoai(jtfDienThoai.getText());
            ch.setNganhang(jtfNganHang.getText());
            ch.setMasothue(jtfMaSoThue.getText());
            ch.setSotaikhoan(jtfSoTaiKhoan.getText());
            ch.setFax(jtfFax.getText());
            ch.setWebsite(jtfWebsite.getText());

            boolean kq = cuahangDAO.capNhatThongTinCuaHang(ch);
            if (kq == true) {
                hienThi();
                JOptionPane.showMessageDialog(this, "Cập Nhật cửa hàng thành công");
            } else {
                JOptionPane.showMessageDialog(this, "Cập Nhật cửa hàng thất bại");
            }
        }
    }//GEN-LAST:event_jbtSuaActionPerformed
    public void Clear() {
        jtfMaCuaHang.setText("");
        jtfTenCuaHang.setText("");
        jtfDiaChi.setText("");
        jtfFax.setText("");
        jtfEmail.setText("");
        jtfWebsite.setText("");
        jtfMaSoThue.setText("");
        jtfNganHang.setText("");
        jtfDienThoai.setText("");
        jtfSoTaiKhoan.setText("");
    }
    private void jbtXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaActionPerformed
        // TODO add your handling code here:
        int maCuaHang = Integer.parseInt(jtfMaCuaHang.getText());
        Cuahang ch = cuahangDAO.layThongTinCuaHang(maCuaHang);

        if (ch != null) {
            ch.setKichhoat(false);
            boolean kq = cuahangDAO.capNhatThongTinCuaHang(ch);
            if (kq == true) {
                hienThi();
                JOptionPane.showMessageDialog(this, "Xóa cửa hàng thành công");
            } else {
                JOptionPane.showMessageDialog(this, "Xóa cửa hàng thất bại");
            }
        }
    }//GEN-LAST:event_jbtXoaActionPerformed

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        // TODO add your handling code here:

        //Kiem tra thong tin nhap
        if (KiemTraThongTinNhap() == false) {
            JOptionPane.showMessageDialog(this, "Chưa Nhập Đủ Thông Tin\nChỉ duy nhất mục Website có thể bỏ trống.", "Thông Báo", JOptionPane.WARNING_MESSAGE);
            return;
        }

        Cuahang ch = new Cuahang();
        ch.setTencuahang(jtfTenCuaHang.getText());
        ch.setDiachi(jtfDiaChi.getText());
        ch.setEmail(jtfEmail.getText());
        ch.setDienthoai(jtfDienThoai.getText());
        ch.setNganhang(jtfNganHang.getText());
        ch.setMasothue(jtfMaSoThue.getText());
        ch.setSotaikhoan(jtfSoTaiKhoan.getText());
        ch.setFax(jtfFax.getText());
        ch.setWebsite(jtfWebsite.getText());
        ch.setKhuvuc(khuvucDAO.layKhuVucTheoTen(this.jcbKhuVuc.getSelectedItem().toString()));
        ch.setKichhoat(true);
        boolean kq = cuahangDAO.themCuaHang(ch);
        if (kq == true) {
            hienThi();
            JOptionPane.showMessageDialog(this, "Thêm cửa hàng thành công");
        } else {
            JOptionPane.showMessageDialog(this, "Thêm cửa hàng thất bại");
        }

    }//GEN-LAST:event_jbtThemActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        loadcomboxKhuVuc();
        hienThi();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jtbDanhSachCuaHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbDanhSachCuaHangMouseClicked
        // TODO add your handling code here:

        int dong = this.jtbDanhSachCuaHang.getSelectedRow();
        Cuahang ch = dsCuaHang.get(dong);
        
        this.jtfMaCuaHang.setText(String.valueOf(ch.getMacuahang()));
        _maCuaHang = ch.getMacuahang();
        jcbKhuVuc.setSelectedIndex(indexCbKhuVucTuTable(ch));
        jtfTenCuaHang.setText(ch.getTencuahang());
        jtfDiaChi.setText(ch.getDiachi());
        jtfDienThoai.setText(ch.getDienthoai());
        jtfFax.setText(ch.getFax());
        jtfEmail.setText(ch.getEmail());
        jtfWebsite.setText(ch.getWebsite());
        jtfMaSoThue.setText(ch.getMasothue());
        jtfSoTaiKhoan.setText(ch.getSotaikhoan());
        jtfNganHang.setText(ch.getNganhang());
        
      
    }//GEN-LAST:event_jtbDanhSachCuaHangMouseClicked

    private void jcbKhuVucActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbKhuVucActionPerformed
        // TODO add your handling code here:
        //   kvClick = jcbKhuVuc.;
    }//GEN-LAST:event_jcbKhuVucActionPerformed

    private void jbtLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLoadActionPerformed
        // TODO add your handling code here:
        hienThi();
    }//GEN-LAST:event_jbtLoadActionPerformed

    private void jbtClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtClearActionPerformed
        // TODO add your handling code here:
        jtfTenCuaHang.setText("");
        jtfDiaChi.setText("");
        jtfDienThoai.setText("");
        jtfFax.setText("");
        jtfEmail.setText("");

        jtfWebsite.setText("");
        jtfMaSoThue.setText("");
        jtfSoTaiKhoan.setText("");
        jtfNganHang.setText("");

    }//GEN-LAST:event_jbtClearActionPerformed
    public boolean hienThiTimKiem(){
        jtbDanhSachCuaHang.removeAll();
        List<Cuahang> ds = cuahangDAO.timKiemCuaHang(_teampObjectCuaHang);
        dtm.setNumRows(ds.size());
        for (int i = 0; i < ds.size(); i++) {
            Cuahang ch = ds.get(i);
            dtm.setValueAt(ch.getMacuahang(), i, 0);
            dtm.setValueAt(ch.getTencuahang(), i, 1);
            dtm.setValueAt(ch.getKhuvuc().getTenkhuvuc(), i, 2);
            dtm.setValueAt(ch.getDiachi(), i, 3);
            dtm.setValueAt(ch.getDienthoai(), i, 4);
            dtm.setValueAt(ch.getFax(), i, 5);
            dtm.setValueAt(ch.getEmail(), i, 6);
            dtm.setValueAt(ch.getWebsite(), i, 7);
            dtm.setValueAt(ch.getMasothue(), i, 8);
            dtm.setValueAt(ch.getSotaikhoan(), i, 9);
            dtm.setValueAt(ch.getNganhang(), i, 10);
        }
        this.jtbDanhSachCuaHang.setModel(dtm);
        this.jtbDanhSachCuaHang.changeSelection(0, 0, false, false);
        return true;
    }
    private void jbtTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtTimKiemActionPerformed
        // TODO add your handling code here:
        _teampObjectCuaHang = new Cuahang();
        if (jtfMaCuaHang.getText().length() == 0) {
            _teampObjectCuaHang.setMacuahang(-1);
        } else {
            _teampObjectCuaHang.setMacuahang(Integer.parseInt(jtfMaCuaHang.getText()));
        }

        Khuvuc kv = (Khuvuc) jcbKhuVuc.getItemAt(jcbKhuVuc.getSelectedIndex());
        _teampObjectCuaHang.setKhuvuc(kv);

        if (jtfTenCuaHang.getText().length() == 0) {
            _teampObjectCuaHang.setTencuahang("");
        } else {
            _teampObjectCuaHang.setTencuahang(jtfTenCuaHang.getText());
        }

        if (jtfDiaChi.getText().length() == 0) {
            _teampObjectCuaHang.setDiachi("");
        } else {
            _teampObjectCuaHang.setDiachi(jtfDiaChi.getText());
        }

        if (jtfDienThoai.getText().length() == 0) {
            _teampObjectCuaHang.setDienthoai("");
        } else {
            _teampObjectCuaHang.setDienthoai(jtfDienThoai.getText());
        }

        if (jtfFax.getText().length() == 0) {
            _teampObjectCuaHang.setFax("");
        } else {
            _teampObjectCuaHang.setFax(jtfFax.getText());
        }

        if (jtfEmail.getText().length() == 0) {
            _teampObjectCuaHang.setEmail("");
        } else {
            _teampObjectCuaHang.setEmail(jtfEmail.getText());
        }

        if (jtfWebsite.getText().length() == 0) {
            _teampObjectCuaHang.setWebsite("");
        } else {
            _teampObjectCuaHang.setWebsite(jtfWebsite.getText());
        }

        if (jtfMaSoThue.getText().length() == 0) {
            _teampObjectCuaHang.setMasothue("");
        } else {
            _teampObjectCuaHang.setMasothue(jtfMaSoThue.getText());
        }

        if (jtfSoTaiKhoan.getText().length() == 0) {
            _teampObjectCuaHang.setSotaikhoan("");
        } else {
            _teampObjectCuaHang.setSotaikhoan(jtfSoTaiKhoan.getText());
        }

        if (jtfNganHang.getText().length() == 0) {
            _teampObjectCuaHang.setNganhang("");
        } else {
            _teampObjectCuaHang.setNganhang(jtfNganHang.getText());
        }
        
          if (hienThiTimKiem() == false) { 
              JOptionPane.showMessageDialog(this,"Không có sản phẩm."); 
          } else {
         
          }
         
    }//GEN-LAST:event_jbtTimKiemActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtClear;
    private javax.swing.JButton jbtLoad;
    private javax.swing.JButton jbtSua;
    private javax.swing.JButton jbtThem;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JButton jbtTimKiem;
    private javax.swing.JButton jbtXoa;
    private javax.swing.JComboBox jcbKhuVuc;
    private javax.swing.JTable jtbDanhSachCuaHang;
    private javax.swing.JTextField jtfDiaChi;
    private javax.swing.JTextField jtfDienThoai;
    private javax.swing.JTextField jtfEmail;
    private javax.swing.JTextField jtfFax;
    private javax.swing.JTextField jtfMaCuaHang;
    private javax.swing.JTextField jtfMaSoThue;
    private javax.swing.JTextField jtfNganHang;
    private javax.swing.JTextField jtfSoTaiKhoan;
    private javax.swing.JTextField jtfTenCuaHang;
    private javax.swing.JTextField jtfWebsite;
    // End of variables declaration//GEN-END:variables
}
